package ClassPackage;


import java.io.Serializable;
import java.util.ArrayList;

import java.util.HashSet;

import java.util.List; 

import javax.persistence.Id;

import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import org.hibernate.annotations.Entity;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Pipaillon
 */
@Entity
public class Film implements Serializable {

    @Id
    private int ID;

    private Set<Personne> director = new HashSet<Personne>();
    private Set<Personne> actor = new HashSet<Personne>();
    
    private Genre genre;

    private String titre;
    private String shortSummary;
    private Date dateSortie;


    private String url_small_picture;
    private String url_big_picture;
    private String url_film;
    private int cost;
    private int nbachat;


    public Film()
    {
        
    }

    public Film(String titre, int cost)
    {
        this.titre=titre;
        this.cost=cost;
    }

    /**
     * @return the ID
     */
    public int getID() {
        return ID;
    }

    /**
     * @param ID the ID to set
     */
    public void setID(int ID) {
        this.ID = ID;
    }

    /**
     * @return the titre
     */
    public String getTitre() {
        return titre;
    }

    /**
     * @param titre the titre to set
     */
    public void setTitre(String titre) {
        this.titre = titre;
    }

    /**
     * @return the shortSummary
     */
    public String getShortSummary() {
        return shortSummary;
    }

    /**
     * @param shortSummary the shortSummary to set
     */
    public void setShortSummary(String shortSummary) {
        this.shortSummary = shortSummary;
    }

    /**
     * @return the dateSortie
     */
    public Date getDateSortie() {
        return dateSortie;
    }

    /**
     * @param dateSortie the dateSortie to set
     */
    public void setDateSortie(Date dateSortie) {
        this.dateSortie = dateSortie;
    }

    /**
     * @return the url_small_picture
     */
    public String getUrl_small_picture() {
        return url_small_picture;
    }

    /**
     * @param url_small_picture the url_small_picture to set
     */
    public void setUrl_small_picture(String url_small_picture) {
        this.url_small_picture = url_small_picture;
    }

    /**
     * @return the url_big_picture
     */
    public String getUrl_big_picture() {
        return url_big_picture;
    }

    /**
     * @param url_big_picture the url_big_picture to set
     */
    public void setUrl_big_picture(String url_big_picture) {
        this.url_big_picture = url_big_picture;
    }

    /**
     * @return the url_film
     */
    public String getUrl_film() {
        return url_film;
    }

    /**
     * @param url_film the url_film to set
     */
    public void setUrl_film(String url_film) {
        this.url_film = url_film;
    }

    /**
     * @return the cost
     */
    public int getCost() {
        return cost;
    }

    /**
     * @param cost the cost to set
     */
    public void setCost(int cost) {
        this.cost = cost;
    }

    /**
     * @return the director
     */
    public Set<Personne> getDirector() {
        return director;
    }

    /**
     * @param director the director to set
     */
    public void setDirector(Set<Personne> director) {
        this.director.addAll(director);
    }

    /**
     * @return the actor
     */
    public Set<Personne> getActor() {
        return actor;
    }

    /**
     * @param actor the actor to set
     */
    public void setActor(Set<Personne> actor) {
        this.actor.addAll(actor);
    }

    public void setDirector(Personne director) {
        this.director.add(director);
    }
      
    /**
     * @param actor the actor to set
     */
    public void setActor(Personne actor) {
        this.actor.add(actor);
    }


    public List getListFilm(SessionFactory cfg, String Title,String Realisateur,String Genre )
    {
        Session hibernate =cfg.getCurrentSession();
        hibernate.getTransaction();
        Query query; 

        boolean is_director_here=false;
        
         String requete= "from Film";

         boolean test=false;
         boolean head= false;
    

         
         if(Title!=null)
         {
             Title = "%" + Title + "%";
             requete+= " f where f.titre like :atitle";
             head=true;
         }
         Genre gen;
         gen = new Genre();


         if(Genre!=null)
         {
         if(head == false)
          {
            if(Genre.compareTo("Action")==0)
            {
               gen= (Genre) hibernate.get(Genre.class,1);
            }
               else
                    if (Genre.compareTo("Comique")==0)
                        gen = (Genre) hibernate.get(Genre.class,2);
                    else
                        if (Genre.compareTo("Horreur")==0)
                            gen =(Genre) hibernate.get(Genre.class,3);
                        else
                            if (Genre.compareTo("Drame")==0)
                                gen =(Genre)hibernate.get(Genre.class,4);
             
              requete+= " f where f.genre = :agenre";
          }else
          {

            if(Genre.compareTo("Action")==0)
            {
               gen= (Genre) hibernate.get(Genre.class,1);
            }
               else
                    if (Genre.compareTo("Comique")==0)
                        gen = (Genre) hibernate.get(Genre.class,2);
                    else
                        if (Genre.compareTo("Horreur")==0)
                            gen =(Genre) hibernate.get(Genre.class,3);
                        else
                            if (Genre.compareTo("Drame")==0)
                                gen =(Genre)hibernate.get(Genre.class,4);
              
              requete+= " and  f.genre = :agenre";
          }
         }
         
         if(Realisateur!=null)
         {
             Realisateur = "%" + Realisateur + "%";
         }

         query = hibernate.createQuery(requete);

         if(head)
            query.setString("atitle",Title);
         if(Genre!=null)
             query.setInteger("agenre",gen.getID());

         List<Film> results = query.list();

         if(results.isEmpty())
         {
             // retourne une liste vide
             return new ArrayList();
         }
         else
         {/*
           if(Realisateur !=null && Realisateur.compareTo("none")==0)
           {
                Iterator it = results.iterator();
                Film tmp_film;
                Set set_director;
                while(it.hasNext())
                {
                    tmp_film=(Film)it.next();
                    set_director = tmp_film.getDirector();

                    if(!set_director.isEmpty())
                    {
                        Iterator it_dir = set_director.iterator();
                        while(it_dir.hasNext())
                        {
                            // si c'est pas le bon réalisateur
                          if(  ((Personne)it_dir).getName().compareTo(Realisateur)==0 )
                          {
                              String tmp;
                              tmp.
                                is_director_here=true;
                          }
                           if(  ((Personne)it_dir).getLastName().compareTo(Realisateur)==0 )
                          {
                                is_director_here=true;
                          }
                        }
                          // SI on a pas trouvé le réalisateur
                          if(!is_director_here)
                          {
                                it.remove();
                          }
                    }
                }
           }*/
          //  Utilisateur tmp = (Utilisateur)it.next();
            return results;
         }

/*
         if(test)
        query.setString("direct",Realisateur);
*/
       
        
    }

    /**
     * @return the genre
     */
    public Genre getGenre() {
        return genre;
    }

    /**
     * @param genre the genre to set
     */
    public void setGenre(Genre genre) {
        this.genre = genre;
    }

    /**
     * @return the nbachat
     */
    public int getNbachat() {
        return nbachat;
    }

    /**
     * @param nbachat the nbachat to set
     */
    public void setNbachat(int nbachat) {
        this.nbachat = nbachat;
    }


}
